/**----------------------------------------------------------*/
/** JS:  cmi_brand_list.js（商品管理--商品品牌）           */
/**----------------------------------------------------------*/

var tipc = 'GOODS_BRAND';
var win = null;

var add = function() {
	try {
		var div = getEditDiv(null);
		win = MyUtils._getToolbarWin("dataPanel", "editform", div, submitAdd, panel + "--添加","保存提交");
		win.window({
			width:790,
			height:380,
			modal: true
		});
		repainBrand(null);
	} catch (e) {
		showJsException(e);
	}
};

var modify = function() {
	try{
		var record = datagridOneRow('dataPanel');
		if(record == null) {
			return;
		}
		var div = getEditDiv(record);
		win = MyUtils._getToolbarWin("dataPanel", "editform", div, submitModify, panel +"--修改","保存提交");
		win.window({
			width:790,
			height:380,
			modal: true
		});
		repainBrand(record);
	} catch(e) {
		showJsException(e);
	}
};

var remove = function() {
	try{
		var rows = $('#dataPanel').datagrid("getSelections");
		if(rows.length == 0) {
			MyUtils.waring(_empty_select);
			return;
		}
		var ids = new Array();
		var names = new Array();
		for(var i = 0; i < rows.length; i++) {
			ids.push(rows[i].uuid);
			names.push(rows[i].brandName);
		}
		var data = { data:"["+MyUtils.createQueryConditionBean("uuid","customer","uuid IN (" + ids.toString() + ")","0",null) +"]"
			    ,st:tipc
			    ,msg:names.toString()
		};
		$.messager.confirm(_msg_sys,MyUtils.valueAlign(_remove_confirm,"left"), function(r) {
			if (r) {
				MyUtils.openMask(_msg_process,$('#t2'));
				MyUtils.sendPost("impl_remove/backRemove.adm",data,function(rets){
					try{
						MyUtils.hiddenMask($('#t2'));
						var res = MyUtils.toJsonObject(rets);
						if(res.error == '0') {
							MyUtils.info(res.msg);
							$('#dataPanel').datagrid("clearSelections");
							$('#dataPanel').datagrid("reload");
						} else {
							MyUtils.error(res.msg);
						}
					} catch (e) {
						showJsException(e);
					}
				});
			}
		});
		
	} catch(e) {
		showJsException(e);
	}
};

var dbclick = function(index,row){
	$('#dataPanel').datagrid("clearSelections");
	$('#dataPanel').datagrid("selectRow",index);
	modify();
};

var initPanel = function() {
	var columns = [  {field : 'brandNameLogo',title : '品牌名称',width : 150,align : 'center',formatter:function(value){
						var vals = value.split(",");
						var div ='<div>'
							+'<label style="float: left;">'+vals[0]+'</label>';
						if(MyUtils.trim(vals[1]).length > 0) {
							div +='<a href="'+(picPath + MyUtils.trim(vals[1])) +'" target="_Blank" style="text-decoration: none;">'
							+'<img alt="" style="float:right;" src="'+flgPic+'" border="0">'
							+'</a>';
						}
						div = div +'</div>';	
						return MyUtils.valueAlign(div,"left");
					}}
					,{field : 'brandNet',title : '品牌网址',width : 250,align : 'center',formatter:function(value){
						if(value.length ==0) {
							value = "http://";
						} else {
							if(value.indexOf("http://") != 0) {
								value = "http://" + value;
							}
						}
						value = '<a href="'+value+'"  target="_Blank">'+value+'</a>';
						return MyUtils.valueAlign(value,"left");
					 }}
					,{field : 'brandDesc',title : '品牌描述',width : 250,align : 'center',formatter:function(value){return MyUtils.valueAlign(value,"left");}}
					,{field : 'brandSort',title : '排序',width : 100,align : 'center',formatter:function(value){return MyUtils.valueAlign(value,"center");}}
					,{field : 'brandShow',title : '是否显示',width : 100,align : 'center',formatter:function(value){
						return '<img alt="OK" src="'+(value =='0' ? noPic:okPic) +'" style="border: 0px;">';
					}}
					,{field : 'brandName',title : '品牌名称',hidden:true}
					];
	var pam = {qr:"[]",st:tipc,sco:'',dc:Dmlx.sort.asc};
	MyUtils.createPaginPanelWithQueryparams('dataPanel',null,Dmlx._hFrozenColumns,columns,toolbars,dbclick, panel,null);
	$('#dataPanel').datagrid({
		url:"impl_search/commonsPaginSearch.ajax",
		queryParams:pam,
		pagination:true,
		fitColumns:true
	});
};

$(function(){
	try {
		initPanel();
		$('#search').linkbutton({
			plain : true
		}).click(function(){
			var brandName = $('#searchBrand').val();
			var pam = {qr:"[]",st:tipc,sco:'',dc:Dmlx.sort.asc};
			if(!MyUtils.isEmpty(brandName)) {
				pam = {qr:"["+MyUtils.createQueryConditionBean("brandName","LIKE","?", brandName,null) +"]",st:tipc,sco:'',dc:Dmlx.sort.desc};
			} 
			MyUtils._searchPanelData('dataPanel', 'impl_search/commonsPaginSearch.ajax', pam);
		});
	} catch (e) {
		showJsException(e,"数据加载失败!");
	}	
});

var submitAdd = function(){
	try {
		var dt = editValidator();
		if(dt != null) {
			MyUtils.openMask(_msg_process,win);
			$('#dt').val(dt);
			$('#dateEdit').attr("action",'impl_add/backFileUploadAdd.adm');
			$('#dateEdit').submit();
		}
	} catch (e) {
		MyUtils.hiddenMask(win);
		showJsException(e,_msg_fail);
	}
};

var submitModify = function(){
	try {
		var dt = editValidator();
		if(dt != null) {
			$.messager.confirm(_msg_sys,MyUtils.valueAlign(_modify_confirm,"left"), function(r) {
				if(r) {
					MyUtils.openMask(_msg_process,win);
					$('#dt').val(dt);
					$('#dateEdit').attr("action",'impl_modify/backFileUploadModify.adm?qy=');
					$('#dateEdit').submit();
				}
			});
		}
	} catch (e) {
		MyUtils.hiddenMask(win);
		showJsException(e,_msg_fail);
	}
};

var editValidator = function(){
	var record = {
			 uuid:$('#uuid').val()
			,brandName:$('#brandName').val()
			,brandDesc:$('#brandDesc').val()
			,brandSort:$('#brandSort').val()
			,brandNet:$('#brandNet').val()
			,brandLogo:$('#brandLogo').val()
			,brandShow:$('input[name="brandShow"]:checked').val()
			,lastBrandlogo:$('#lastBrandlogo').val()
		};
	
	//验证用户账号
	if(MyUtils.isEmpty(record.brandName)) {
		MyUtils.waring("品牌名称不能为空");
		return null;
	}
	return MyUtils.toJsonStr(record);
};

var submitDate = function(url,data) {
	MyUtils.sendPost(url,data,function(msg){
		try{
			MyUtils.hiddenMask(win);
			var res = MyUtils.toJsonObject(msg);
			if(res.error == '0') {
				if(win)win.window('close');
				$('#dataPanel').datagrid("clearSelections");
				$('#dataPanel').datagrid("reload");
			} else {
				MyUtils.error(res.msg);
			}
		} catch (e) {
			MyUtils.hiddenMask(win);
			MyUtils.error(_msg_fail);
		}
	});
};

var getEditDiv = function(record) {
	var div= '<div id="editform"><iframe name="ifrName" src="" id="ifrName" style="display:none;"></iframe><form target="ifrName" action="" method="POST" name="fmName" id="dateEdit" enctype="multipart/form-data">'
		+'<table cellspacing="2" cellpadding="2">'
		+'<tbody>'
		+'<tr>'
		+'<th align="right" width=200>品牌名称：</th>'
		+'<td align="left" width=500>'
		+'<input type="hidden" id="uuid" value="" >'
		+'<input type="text" maxlength="120" id="brandName" style="width: 288px;" value=""><label class="requre_field">*</label>'
		+'</td>'
		+'</tr>'
		+'<tr>'
		+'<th align="right" width=200>品牌网址：</th>'
		+'<td align="left" width=500>'
		+'<input type="text" id="brandNet" style="width: 288px;" value="">'
		+'</td>'
		+'</tr>'
		+'<tr>'
		+'<th align="right" valign="top" width=200>品牌LOGO：</th>'
		+'<td align="left" width=500>'
		+'<input type="file" name="file" id="brandLogo" style="width: 288px;" value="" size =40>'
		+'<div style="color:gray;">请上传图片，做为品牌的LOGO！</div>	'
		+'</td>'
		+'</tr>'
		+'<tr>'
		+'<th align="right" valign="top" width=200>品牌描述：</th>'
		+'<td align="left"><textarea rows="6" cols="31" id="brandDesc" style="width: 300px;" ></textarea></td>'
		+'</tr>'
		+'<tr>'
		+'<th align="right" width=200>排序：</th>'
		+'<td align="left" width=500>'
		+'<input type="text"  maxlength="5" id="brandSort" style="width: 288px;" value="50">'
		+'</td>'
		+'</tr>'
		+'<tr>'
		+'<th align="right" width=200>是否显示：</th>'
		+'<td align="left">'
		+'<input type = "radio" name="brandShow"  value="1"	>是</input>'
		+'<input type = "radio" name="brandShow"  value="0"	>否</input>'
		+'<div style="color:gray;">当品牌下还没有商品的时候，首页及分类页的品牌区将不会显示该品牌。</div>	'		
		+'</td>'
		+'</tr>'
		+'</tbody>'
		+'</table>'
		+'<input type="hidden" id="lastBrandlogo" value="" >'
		+'<input type="hidden" id="dt" name="dt" value="" >'
		+'<input type="hidden" id="st" name="st" value="'+tipc+'" >'
		+'</form><div>';
	return div;
};

var repainBrand = function(record){
	$('input[name="brandShow"][value='+(record ? record.brandShow : 1)+']').attr("checked",true);
	if(record) {
		$('#uuid').val(record.uuid);
		$('#brandName').val(record.brandName);
		$('#brandDesc').val(record.brandDesc);
		$('#brandSort').val(record.brandSort);
		$('#brandNet').val(record.brandNet);
		$('#lastBrandlogo').val(record.brandLogo);
	}
	$('#ifrName').load(function() {
		try {
			MyUtils.hiddenMask(win);
			 var response = this.contentWindow.document.body.textContent;
			 if(!MyUtils.isEmpty(response)){
				var res = MyUtils.toJsonObject(response);
				if(res.error == '0') {
					MyUtils.info(res.msg);
					if(win)win.window('close');
					$('#dataPanel').datagrid("clearSelections");
					$('#dataPanel').datagrid("reload");
				} else {
					MyUtils.error(res.msg);
				}
			 }
		} catch (e) {
			MyUtils.hiddenMask(win);
			showJsException(e,_msg_fail);
		}
	});
};
